package com.newsystem.service.impl;

import com.newsystem.MyUtils.CommonResult;
import com.newsystem.MyUtils.MyUUIDUtils;
import com.newsystem.mapper.UserMapper;
import com.newsystem.pojo.GeneralUser;
import com.newsystem.mapper.GeneralUserMapper;
import com.newsystem.pojo.User;
import com.newsystem.pojo.vo.GeneralUserInfo;
import com.newsystem.service.IGeneralUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenlitao
 * @since 2022-11-06
 */
@Service
public class GeneralUserService extends ServiceImpl<GeneralUserMapper, GeneralUser> implements IGeneralUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GeneralUserMapper generalUserMapper;

    //普通用户注册
    public CommonResult<Object> insertOneGeneralUser(User user,GeneralUser generalUser){
        List<User> users = userMapper.selectByAccount(user.getAccount());
        if(users.size()>0){
            return CommonResult.failed("此账号已存在");
        }
        else {
            user.setId(MyUUIDUtils.getUUID());
            user.setStatue(1);
            if(userMapper.insert(user)>0){
                generalUser.setUserId(user.getId());
                generalUser.setId(MyUUIDUtils.getUUID());
                if(generalUserMapper.insert(generalUser)>0){
                    return CommonResult.success("注册成功");
                }
                else {
                    userMapper.deleteById(user.getId());
                }
            }
            return CommonResult.failed("注册失败");
        }
    }


    //获取自己的个人信息
    public CommonResult<Object> selectGeneralUsrInfoById(String generalUserId){
        GeneralUserInfo generalUserInfo = generalUserMapper.selectByGeneralId(generalUserId);
        if(generalUserInfo==null){
            return CommonResult.failed("获取失败");
        }
        if(generalUserInfo.getUser().getStatue()!=1){
            return CommonResult.failed("此账号已经注销");
        }
        return CommonResult.success(generalUserInfo);
    }





}
